package com.wedo.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.adapter.Adapter;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.listener.EListener;
import com.wedo.mall.R;
import com.wedo.mall.activity.LoginActivity;
import com.wedo.mall.activity.goods.GoodsDetailActivity;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.util.WUrl;
import com.wedo.mall.util.WUtils;

import java.text.DecimalFormat;
import java.util.List;

import io.reactivex.Observable;

/**
 * Created by lxy on 2017/6/19.
 */

public class FavourableAdapter extends Adapter<Goods> {

    //private String goods_sale;
    private String goods_price;

    public FavourableAdapter(Context context, List<Goods> datas) {
        super(context, datas);
        Resources res = context.getResources();
        //goods_sale = res.getString(R.string.goods_sale);
        goods_price = res.getString(R.string.goods_price);
    }

    @Override
    protected int layout() {
        return R.layout.item_favourable;
    }

    @Override
    protected void bind(EHolder holder, Goods goods, int position) {
        ImageView iv_img = holder.find(R.id.iv_img);
        ImageView iv_auth = holder.find(R.id.iv_auth);
        TextView tv_name= holder.find(R.id.tv_name);
        TextView tv_price = holder.find(R.id.tv_price);
        TextView tv_sale = holder.find(R.id.tv_sale);
        TextView tv_favorable_price = holder.find(R.id.tv_favorable_price);
        TextView tv_cost = holder.find(R.id.tv_cost);

        ImageView iv_shopping = holder.find(R.id.iv_shopping);
        setShoppingListener(iv_shopping,goods);

        iv_auth.setVisibility(goods.auth()?View.VISIBLE:View.INVISIBLE);

        tv_name.setText(goods.goods_name);
        //tv_sale.setText(String.format(goods_sale,goods.sale_number));


        if(goods.is_favorable == 1) {
            /**原价*/
            tv_price.setVisibility(View.VISIBLE);
            tv_cost.setVisibility(View.VISIBLE);
            tv_price.setText(goods.goods_price);
            tv_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            /**优惠价*/
            tv_favorable_price.setText(String.format(goods_price,goods.fixPrice()));
            /**优惠百分比*/
            tv_cost.setText(favorablePercent(goods));
        }else {
            tv_price.setVisibility(View.GONE);
            tv_cost.setVisibility(View.GONE);
            /**无优惠，显示原价*/
            tv_favorable_price.setText(String.format(goods_price,goods.fixPrice()));

        }


        String img = WUrl.url(goods.goods_image);
        Glide.with(mContext).load(img).error(R.drawable.place_holder_goods).placeholder(R.drawable.place_holder_goods).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_img);
    }

    private String favorablePercent(Goods goods){
        try {
            double price = WUtils.toDouble(goods.goods_price);
            double favorable_price = WUtils.toDouble(goods.favorable_price);

            double cost = price - favorable_price;
            if(cost <=0) {
                return "0%";
            }

            double percent = cost/price * 100;
            DecimalFormat df = new DecimalFormat("#.00");
            String value = df.format(percent);
            StringBuilder sb = new StringBuilder();
            sb.append("-").append(value).append("%");
            return sb.toString();
        }catch (Exception e){
            e.printStackTrace();
            return "0%";
        }
    }

    public void listener(final EActivity activity){
        onItemClickListener = new EListener<Goods>() {
            @Override
            public void onInvoked(View view, Goods data, int position) {
                GoodsDetailActivity.toDetail(activity,data.id);
            }
        };
    }

    private void setShoppingListener(ImageView iv_shopping, final Goods goods){
        iv_shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shopping(goods);
            }
        });
    }

    /**加入购物车*/
    private void shopping(Goods goods){

        if(!goods.payable()) {
            WUtils.showTipsDialog(mContext);
            return;
        }

        if(!WLogin.logged(mContext)) {
            Intent intent = new Intent(mContext, LoginActivity.class);
            intent.putExtra(LoginActivity.EXTRA_ENDPOINT,LoginActivity.ENDPOINT_MAIN);
            mContext.startActivity(intent);
            return;
        }

        if (!WLogin.isBinding(mContext)){
            return;
        }

        Observable<EResponse> observable = EClient.with().api().addShoppingCar(goods.id,"1","0");
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                if (mOnShoppingListener !=null){
                    mOnShoppingListener.onAdd2Car(1);
                }
                Toast.makeText(mContext,R.string.goods_add_to_car_successful,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failure(String msg) {
                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private GoodsAdapter.OnShoppingListener mOnShoppingListener;

    public void setOnShoppingListener(GoodsAdapter.OnShoppingListener onShoppingListener) {
        this.mOnShoppingListener = onShoppingListener;
    }
}
